﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MaiPlanServer.Utility
{
    public  class OrderCode
    {

        /// <summary>
        /// 当前单数
        /// </summary>
        public static string OrderNumber { get; set; }

        /// <summary>
        /// 获取订单编码
        /// </summary>
        /// <param name="CodeName">前缀编码</param>
        /// <param name="orderDate">订单编号</param>
        /// <returns></returns>
        public static string GetOrderCode(string CodeName,string orderDate)
        {
            DateTime currentDate = DateTime.Now;

            if (!string.IsNullOrEmpty(orderDate))
            {
                var orderArr = orderDate.Split('-');
                var bol = orderArr[orderArr.Length - 1].Length == 3;
                if (!bol)
                    OrderNumber = "000";
                else
                    OrderNumber = orderArr[orderArr.Length - 1];
            }
            else
            {
                OrderNumber = "000";
            }

            // 解析订单号
            int orderInt = 0;
            if (Int32.TryParse(OrderNumber, out orderInt))
            {
                // 计算日期偏移量
                int dateOffset = orderInt - 1;
                currentDate = currentDate.AddDays(dateOffset);

                // 获得新的订单号
                OrderNumber = (orderInt + 1).ToString("D3");
            }

            // 生成新的订单号和日期字符串
            string newDateStr = currentDate.ToString("yyyy-MM-dd");
            string newOrderCode = CodeName + newDateStr + "-" + OrderNumber;

            //返回订单编号
            return newOrderCode;
        }



    }
}
